﻿using TbShop.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TbShop.Business.Components
{
    public class SiteMapBC : ServiceComponents<SiteMap>, IBusinessContractBase<SiteMap>
    {
        /// <summary>
        /// 根据用户ID获取菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IEnumerable<SiteMap> GetAllSiteMap(string userId)
        {
            var data = from s in CurrentDBContext.SiteMaps where !s.IsDeleted select s;
            //var data = from s in CurrentDBContext.SiteMaps
            //           where s.IsEnable && !s.IsDeleted
            //           && (from a in CurrentDBContext.Authoritys
            //               join r in CurrentDBContext.RoleAuthoritys on a.Id equals r.AuthorityId
            //               join ur in CurrentDBContext.AspNetUserRoles on r.RoleId equals ur.RoleId
            //               where ur.UserId == userId && !a.IsDeleted && !r.IsDeleted
            //               select a.Code).Contains(s.Code)
            //           select s;

            var parentMaps = data.Where(s => s.ParentId == 0).OrderBy(s => s.OrderNum).ToList();
            foreach (var map in parentMaps)
            {
                var findChildren = data.Where(x => x.ParentId == map.Id).OrderBy(x => x.OrderNum).ToList();
                map.ChildSiteMaps = findChildren;
            }
            return parentMaps;
        }
    }
}
